package com.example.shirotest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shirotest.base.ResultVo;
import com.example.shirotest.console.GetRemoteData;
import com.example.shirotest.entry.Result;
import com.example.shirotest.entry.Sites;
import com.example.shirotest.entry.TableList;
import com.example.shirotest.mapper.ResultMapper;
import com.example.shirotest.service.ResultService;
import com.example.shirotest.service.SitesService;
import com.example.shirotest.utils.DateUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private GetRemoteData getRemoteData;
    
    @Autowired
    private ResultMapper resultMapper;

    @Autowired
    private ResultService resultService;

    @Autowired
    SitesService sitesService;

    @RequestMapping("/home")
    @ResponseBody
    public String home(){
        return "home page " + DateUtil.getCurDateTimeStr();
    }

    @RequestMapping("/getuniquecash")
    @ResponseBody
    public ResultVo test(){
        HashMap uniqueMap = getRemoteData.getUniqueMap();
        return new ResultVo(uniqueMap);
    }
    
    @RequestMapping("/ts")
    @ResponseBody
    public void ts(){
/*        QueryWrapper<Result> query = new QueryWrapper();
        query.eq("id","44");

        List<Result> results = resultMapper.selectList(query);

        results.forEach(item-> System.out.println(item.toString()));*/

    }


    @RequestMapping("/news")
    public String news(Model model){
        List<Sites> list = sitesService.list();
        model.addAttribute("sitelist",list);

        return "index/news";
    }

    @RequestMapping("/newslist")
    @ResponseBody
    public TableList newslist(HttpServletRequest request){
        Integer pagenum  = Integer.parseInt(request.getParameter("pagenum"));
        Integer pagesize  = Integer.parseInt(request.getParameter("pagesize"));
        Integer sid  = Integer.parseInt(request.getParameter("sid"));
        String  sort  = request.getParameter("sort");
        String  order  = request.getParameter("order");
        String  keywords = request.getParameter("keywords");

        PageInfo<Result> PageInfo = resultService.selectByPage(pagenum, pagesize, sort + " " + order,sid,keywords);

        //封装输出数据
        TableList<Result> TableList = new TableList<>();
        TableList.setList(PageInfo.getList());
        TableList.setTotal((int) PageInfo.getTotal());

        return TableList;
    }


}
